<template>
	<view class="main">
		<form @submit="submit">
			<view class="cu-form-group">
				<view class=" iconfont icon-my_light"></view>
				<view class="title icon">姓名</view>
				<input placeholder="输入姓名" name="user"></input>
			</view>
			<view class="cu-form-group">
				<view class=" iconfont icon-profile_light"></view>
				<view class="title icon">学号</view>
				<input placeholder="输入学号" name="code"></input>
			</view>
			<view class="cu-form-group">
				<view class=" icon iconfont icon-read"></view>
				<view class="title">班级</view>
				<picker @change="ClassChange" :value="index1" :range="classes" name="classes">
					<view class="picker">
						{{index1>-1?classes[index1]:'请选择'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class=" iconfont icon-my_light"></view>
				<view class="title icon">密码</view>
				<input placeholder="输入密码" name="password" :password="showPassword"></input>
				<text class="uni-icon" :class="[!showPassword ? 'uni-eye-active' : '']" @click="changePassword">&#xe568;</text>
			</view>
				<button form-type="submit" class="sub">注册</button>
		</form>
	</view>
</template>

<script>
	var  graceChecker = require("../../common/graceChecker.js");
	export default {
		data() {
			return {
				index1:-1,
				classes:["18计算机一班","18计算机二班","18计算机三班","18计算机四班",],
				showPassword: true,
				username:'',
				code:'',
				cl:'',
				pwd:''
			}
		},
		methods: {
			ClassChange(e) {
				this.index1 = e.detail.value
			},
			changePassword: function() {
			    this.showPassword = !this.showPassword;
			},
			submit(e){
				//定义表单规则
				var rule = [
					{name:"user", checkType : "notnull", checkRule:"",  errorMsg:"请输入姓名"},
					{name:"code", checkType : "notnull", checkRule:"",  errorMsg:"请输入学号"},
					
					{name:"password", checkType : "notnull", checkRule:"",  errorMsg:"请输入密码"}
				];
				//进行表单验证
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if(checkRes){
					// console.log(e)
					this.username =e.detail.value.user;
					this.code =e.detail.value.code;
					this.cl =e.detail.value.classes;
					this.pwd =e.detail.value.password;
					//判断是否选择班级
					if(this.cl==-1){
						uni.showToast({
							icon:"none",
						    title: "请选择班级",
						    duration:1000
						});
						return false;
					}
					uni.request({
					    url: '/graduate_api/api_18/login.php', //仅为示例，并非真实接口地址。
						method:"POST",
					    data: {
					        username:this.username,
							code:this.code,
							cl:this.cl,
							pwd:this.pwd,
					    },
					    success: (res) => {
							
					       if(res.data.code==200){
							   uni.showToast({
							       title: "注册成功",
							       duration:1000
							   });
							   setTimeout(function(){
								   uni.redirectTo({
								       url: '../login/login'
								   });
							   },1000);
						   }else{
							   uni.showToast({
							       title: "注册失败",
							       duration:1000,
								   icon: "none"
							   });
						   }
					    }
					});
					
					
				 //    uni.showToast({title:"注册成功！", icon:"none"});
					// uni.reLaunch({
					//     url: '../login/login'
					// });
				}else{
				    uni.showToast({ title: graceChecker.error, icon: "none" });
				}
				
			}
		}
	}
</script>

<style scoped>
	.cu-form-group{
		background: none;
	}
	.main{
		background: url(../../static/img/bj.jpg);
		padding: 20rpx;
		height: 100vh;
	}
	.sub,.res{
		width: 80%;
		height: 80upx;
		background: #63a7ff;
		border-radius: 50upx;
		margin-top: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
	}
	.phcolor{
		color: #63a7ff;
	}
	.iconfont{
		font-size: 24px;
		margin-right: 10px;
		color: #63a7ff;
	}
	
	.video {
		width: 100%;
	}
	.camera-tips {
		padding: 10rpx 30rpx;
	}
	.uni-eye-active {
	    color: #63a7ff;
	}
</style>
